package com.lf.api.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import com.lf.api.exception.SystemException;

public class BaseController {

	private static final Logger logger = LoggerFactory.getLogger(BaseController.class);

	@ExceptionHandler
	public String exp(HttpServletRequest request, Exception ex) {
		String resultViewName = "errors/error";

		// 记录日志
		logger.error(ex.getMessage(), ex);

		// 根据不同错误转向不同页面
		if (ex instanceof SystemException) {
			resultViewName = "errors/biz-error";
		} else {
			// 异常转换
			ex = new Exception("系统太累了，需要休息!");
		}
		request.setAttribute("ex", ex);

		String xRequestedWith = request.getHeader("X-Requested-With");
		if (!StringUtils.isEmpty(xRequestedWith)) {
			// ajax请求
			resultViewName = "errors/ajax-error";

		}

		return resultViewName;
	}
}
